<template>
  <div>
    <el-dialog v-bind="$attrs" v-on="$listeners" @open="onOpen" @close="onClose" :title="mulu" :visible="showDirectory">
      <el-form ref="formRef" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="所属学科" prop="subjectID" v-if="!id">
          <el-select v-model="formData.subjectID" placeholder="请选择所属学科" clearable :style="{width: '100%'}">
            <el-option v-for="item in data" :key="item.id" :label="item.label"
              :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName" >
          <el-input v-model="formData.directoryName" placeholder="请输入目录名称" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-form>
    <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm" ref="addDirectory">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { add } from '@/api/hmmm/directorys'
import { simple } from '@/api/hmmm/subjects'
export default {
  props: {
    showDirectory: {
      type: Boolean,
      default: false
    },
    id: {
    }

  },
  inheritAttrs: false,
  components: {},
  data () {
    return {
      mulu: '新增目录', // 目录显示
      data: [], // 所属科目数据
      formData: {
        subjectID: undefined,
        directoryName: undefined,
        subjectName: undefined
      },
      rules: {
        subjectID: [{
          required: true,
          message: '请选择所属学科',
          trigger: 'change'
        }],
        directoryName: [{
          required: true,
          message: '请输入目录名称',
          trigger: 'blur'
        }]
      },
      subjectIDOptions: [{
        label: '选项一',
        value: 1
      }, {
        label: '选项二',
        value: 2
      }]
    }
  },
  computed: {},
  watch: {},
  async created () {
    const { data } = await simple()
    this.data = data
    // console.log(this.data)
  },
  mounted () {},
  methods: {
    onOpen () {},
    onClose () {
      // 清空校验
      this.$refs.formRef.resetFields()
      // 清空数据
      this.formData = this.$options.data().formData
      // 关闭弹层
      this.close()
    },
    close () {
      this.$emit('update:showDirectory', false)
    },
    // 新增数据
    handelConfirm () {
      this.mulu = '新增目录'
      this.$refs.formRef.validate(async valid => {
        // 提交表单 调用接口
        if (this.id) {
          this.formData.subjectID = Number(this.id)
        }
        // this.formData.subjectID = Number(this.id)
        // console.log(this.formData
        // console.log(this.$parent.formData)
        await add(this.formData)
        this.$message.success('添加成功')
        // 直接调用父组件的方法 通知父组件跟新数据
        this.$parent.list()
        // 关闭弹层
        this.close()
        // 传数据
        // this.$emit('handleChange', this.formData)
        if (!valid) return
        this.close()
      })
    }
  }
}

</script>
<style>
</style>
